<template>
    <div class="login_container">
        <div class="login_box">
            <!-- LOGO区域 -->
            <div class="avater_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- LOGO区域结束 -->
            <!-- 表单样式区域 -->
            <div class="input_box">
                <!-- 表单区域 -->
                <el-form ref="form" :rules="loginFormRules" :model="inputForm" >
                    <!-- 用户名 -->
                    <el-form-item prop="username">
                      <el-input v-model="inputForm.username" prefix-icon="el-icon-s-custom" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                      <el-input v-model="inputForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password ></el-input>
                    </el-form-item>
                </el-form>
                <!-- 按钮区域 -->
                <div class="input_btn">
                    <el-button type="primary" @click="validateForm('form')">登录</el-button>
                    <el-button type="info" @click="resetForm('form')">重置</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

export default ({
  data () {
    return {
      inputForm: {
        username: '',
        password: ''
      },
      //  Form 组件提供了表单验证的功能，只需要通过 rules 属性传入约定的验证规则，并将 Form-Item 的 prop 属性设置为需校验的字段名即可
      loginFormRules: {
        username: [
          { required: true, message: '用户名格式不支持', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码格式错误', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (formName) {
      this.$refs[formName].validate(async (valid, obj) => {
        if (!valid) {
          console.log('error submit!!')
          console.log(obj)
          return false
        } else {
          // 开始调用登录接口
          const { data: res } = await this.$axios.post('login', this.inputForm)
          // console.log(res)
          // 打印登录状态信息
          if (res.meta.status !== 200) return this.$message.error('登录失败')
          this.$message.success('登录成功')
          // 保存token
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('./home')
        }
      })
    },
    resetForm (resetFormName) {
      //  console.log(this)
      this.$refs[resetFormName].resetFields()
      //  对整个表单进行重置，将所有字段值重置为初始值并移除校验结果validate
    }
  }
})

</script>

<style lang="less" scoped>
.login_container {
    background-color:#409EFF;
    height: 100%;
    position: relative;
    width: 100%;
}
.login_box {
    width: 450px;
    height: 300px;
    border-radius: 10px;
    background: #ffffff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    .avater_box{
        width: 130px;
        height:130px;
        border: 2px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #ffffff;
        img {
            border-radius: 50%;
            width: 100%;
            height: 100%;
            background-color: #eee;
        }
    }
}
.input_box {
    position: absolute;
    top: 40%;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
    .input_btn {
        display: flex;
        justify-content: flex-end;
    }
}

</style>
